import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/login/login_controller.dart';
import 'package:kkhousekeep/page/me/about.dart';
import 'package:kkhousekeep/page/me/me_controller.dart';
import 'package:kkhousekeep/widget/item_cell.dart';

class MePage extends StatelessWidget {
  const MePage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MeController>(
      init: MeController(),
      builder: (controller) => Scaffold(
        appBar: null,
        body: Container(
          decoration: const BoxDecoration(
              image: DecorationImage(
            image: AssetImage("assets/images/page_bg.png"),
            fit: BoxFit.fill,
          )),
          child: SafeArea(
            left: false,
            right: false,
            child: Container(
              padding: EdgeInsets.only(left: 32.w, right: 32.w),
              child: Column(
                children: [
                  _buildTitle(controller),
                  SizedBox(
                    height: 34.h,
                  ),
                  _buildInfoWidget(context, controller),
                  SizedBox(
                    height: 34.h,
                  ),
                  _buildItemCellList(context, controller)
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

Widget _buildTitle(MeController controller) {
  return Padding(
    padding: EdgeInsets.all(26.h),
    child: Text(
      "我的",
      style: TextStyle(
        fontSize: 36.sp,
        color: const Color(0xFF333333),
      ),
    ),
  );
}

Widget _buildInfoWidget(BuildContext context, MeController controller) {
  return Container(
    padding: EdgeInsets.only(left: 24.w, right: 24.w),
    decoration: BoxDecoration(
      gradient: const LinearGradient(
        colors: [Color(0xFF3673E8), Color(0xFF6dcce7)],
        begin: Alignment.centerLeft,
        end: Alignment.centerRight,
      ),
      borderRadius: BorderRadius.circular(32.w),
    ),
    constraints: BoxConstraints(minHeight: 176.h, maxHeight: 276.h),
    child: Obx(
      () => Row(
        mainAxisSize: MainAxisSize.max,
        children: [
          Container(
            alignment: Alignment.centerLeft,
            width: 96.w,
            height: 96.h,
            margin: EdgeInsets.only(right: 32.w),
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              border: Border.all(color: const Color(0x33000000), width: 2.r),
              image: const DecorationImage(
                fit: BoxFit.fill,
                image: ExactAssetImage("assets/images/icon_default_avatar.png"),
              ),
            ),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  controller.name.value,
                  style: TextStyle(
                      fontSize: 32.sp,
                      color: Colors.white,
                      fontWeight: FontWeight.bold),
                ).marginOnly(bottom: 12.h),
                Row(children: [
                  (controller.userType.value == UserType.property)
                      ? Container()
                      : Image(
                          width: 30.w,
                          height: 30.h,
                          image: const AssetImage(
                              "assets/images/icon_location.png"),
                        ),
                  Text(
                    controller.description.value,
                    style: TextStyle(fontSize: 24.sp, color: Colors.white),
                  )
                ])
              ],
            ),
          )
        ],
      ),
    ),
  );
}

Widget _buildItemCellList(BuildContext context, MeController controller) {
  return Obx(
    () => Column(
      children: [
        Visibility(
          visible: controller.userType.value != UserType.property,
          child: Column(
            children: [
              ItemCell(
                title: "我的房屋",
                imageName: "assets/images/me_icon_my_house.png",
                hasNewMsg: controller.hasNewMsg.value,
                onTap: () => controller.performClickMyHouse(),
              ),
              SizedBox(height: 2.h),
              ItemCell(
                title: "家庭成员",
                imageName: "assets/images/me_icon_family_member.png",
                onTap: () {
                  controller.buildFamilyMember();
                },
              )
            ],
          ),
        ),
        SizedBox(height: 32.h),
        ItemCell(
          title: "设置",
          imageName: "assets/images/me_icon_setting.png",
          onTap: () {
            controller.buildSettingPage();
          },
        ),
        SizedBox(height: 2.h),
        ItemCell(
          title: "关于",
          imageName: "assets/images/me_icon_about.png",
          onTap: () {
            Get.to(() => const About());
          },
        )
      ],
    ),
  );
}
